package com.ocom.accesservice.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.accesservice.service.MjDeviceInfoService;
import com.ocom.accesservice.service.TimeInfoService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityMjTimeInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.access.TimeInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
public class TimeInfoController {

    @Autowired
    private TimeInfoService timeInfoService;


    @Autowired
    private MjDeviceInfoService mjDeviceInfoService;

    //获取开门时段
    @PostMapping(AccessServiceUrl.URL_getAccessTimeSlots)
    public Result getAccessTimeSlots(@RequestBody @Validated TimeInfoVo timeInfoVo){
        try {
            List<Map<String,Object>> reList=new ArrayList<>();
            List<EntityMjTimeInfo> entityMjTimeInfoList=timeInfoService.getTimeList(timeInfoVo.getCompanyID(),timeInfoVo);
            for (EntityMjTimeInfo entityMjTimeInfo : entityMjTimeInfoList) {
                Map<String,Object> map=new HashMap<>();
                map.put("direction",entityMjTimeInfo.getDirection());
                map.put("beginTime",entityMjTimeInfo.getBeginTime());
                map.put("endTime",entityMjTimeInfo.getEndTime());
                reList.add(map);
            }
            Map<String,Object> reMap=new HashMap<>();
            reMap.put("timeSlots",reList);
            return ResultUtil.success(reMap);
        }catch (Exception e){
            log.info("获取开门时段:"+e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    //设置开门时段
    @PostMapping(AccessServiceUrl.URL_setAccessTimeSlots)
    public Result setAccessTimeSlots(@RequestBody @Validated TimeInfoVo timeInfoVo){
        if (timeInfoVo.getTimeSlots() ==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        timeInfoService.deleAccessTimeSlots(timeInfoVo.getCompanyID(),timeInfoVo);
        List<Map<String,Object>> list= JSON.parseObject(JSON.toJSONString(timeInfoVo.getTimeSlots()),List.class);
        int timeNo=0;
        List<EntityMjTimeInfo> timeInfoList=new ArrayList<>();
        for (Map<String, Object> map : list) {
            timeNo++;
            EntityMjTimeInfo entityMjTimeInfo=new EntityMjTimeInfo();
            entityMjTimeInfo.setComId(timeInfoVo.getCompanyID());
            entityMjTimeInfo.setGroupNo(timeInfoVo.getGroupNo());
            entityMjTimeInfo.setTimeNo((long) timeNo);
            entityMjTimeInfo.setAreaId(Long.valueOf(timeInfoVo.getAreaNo()));
            entityMjTimeInfo.setZoneNo(Long.valueOf(timeInfoVo.getZoneNo()));
            entityMjTimeInfo.setDirection((Integer) map.get("direction"));
            entityMjTimeInfo.setBeginTime(String.valueOf(map.get("beginTime")));
            entityMjTimeInfo.setEndTime(String.valueOf(map.get("endTime")));
            Date date=new Date();
            entityMjTimeInfo.setCreateTime(date);
            entityMjTimeInfo.setUpdateTime(date);
            timeInfoList.add(entityMjTimeInfo);
        }

        Boolean b=timeInfoService.savesEntityMjTimeInfo(timeInfoVo.getCompanyID(),timeInfoList);
        if (b){

            //更新机器权限组标识
            mjDeviceInfoService.updateGroupVer(timeInfoVo.getCompanyID());

            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_2009);
        }
    }
}
